{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备工作\n",
    "公众号 = \"腾讯媒体研究院\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "fn = { \"output\" : { \"公众号_htm_snippets\": \"data_raw_src/公众号_htm_snippets_{公众号}.tsv\",\n",
    "                    \"公众号_df\": \"data_raw_src/公众号_df_{公众号}.tsv\",\n",
    "                    \"公众号_xlsx\": \"data_sets/公众号_url_{公众号}.xlsx\" } \\\n",
    "      }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "5\n",
      "10\n",
      "15\n",
      "20\n",
      "25\n",
      "30\n",
      "35\n",
      "40\n",
      "45\n",
      "50\n",
      "55\n",
      "60\n",
      "65\n",
      "70\n",
      "75\n",
      "80\n",
      "85\n",
      "90\n",
      "95\n",
      "100\n",
      "105\n",
      "110\n",
      "115\n",
      "120\n",
      "125\n",
      "130\n",
      "135\n",
      "140\n",
      "145\n",
      "150\n",
      "155\n",
      "160\n",
      "165\n",
      "170\n",
      "175\n",
      "180\n",
      "185\n",
      "190\n",
      "195\n",
      "200\n",
      "205\n",
      "210\n",
      "215\n",
      "220\n",
      "225\n",
      "230\n",
      "235\n",
      "240\n",
      "245\n",
      "250\n",
      "255\n",
      "260\n",
      "265\n",
      "270\n",
      "275\n",
      "280\n",
      "285\n",
      "290\n",
      "295\n",
      "300\n",
      "305\n",
      "310\n",
      "315\n",
      "320\n",
      "325\n",
      "330\n",
      "335\n",
      "340\n",
      "345\n",
      "350\n",
      "355\n",
      "360\n",
      "365\n",
      "370\n",
      "375\n",
      "380\n",
      "385\n",
      "390\n",
      "395\n",
      "400\n",
      "405\n",
      "410\n",
      "415\n",
      "420\n",
      "425\n",
      "430\n",
      "435\n",
      "440\n",
      "445\n",
      "450\n",
      "455\n",
      "460\n",
      "465\n",
      "470\n",
      "475\n",
      "480\n",
      "485\n",
      "490\n",
      "495\n",
      "500\n",
      "505\n",
      "510\n",
      "515\n",
      "520\n",
      "525\n",
      "530\n",
      "535\n",
      "540\n",
      "545\n",
      "550\n",
      "555\n",
      "560\n",
      "565\n",
      "570\n",
      "575\n",
      "580\n"
     ]
    }
   ],
   "source": [
    "#采集公众号（requests）\n",
    "import time\n",
    "import requests\n",
    "import pandas as pd\n",
    "import csv\n",
    "\n",
    "\n",
    "url = \"https://mp.weixin.qq.com/cgi-bin/appmsg\"\n",
    "\n",
    "# 使用Cookie，跳过登陆操作\n",
    "# 我们自己使用的Cookie，User-Agent，token\n",
    "headers = {\n",
    "  \"Cookie\": \"pgv_pvi=5326464000; RK=LqrgHjopM9; ptcz=5e6f04243dd48789e31d4a8c00a442340f8e27881f27f9cfd7a54d6c0845dd61; pgv_pvid=9651134204; ua_id=Vhv0eUqPPZtErZLmAAAAABHqm7IskBi-0P_yvYVIduo=; mm_lang=zh_CN; wxuin=71557665244067; pac_uid=0_6827b849f58b7; luin=o1219122170; tvfe_boss_uuid=9b8b7f3832071952; o_cookie=1219122170; lskey=000100001da2c9c37a85880ed973e3f4241cd028f7864e9625f4177cdf7484f083e41881fdf82ed328ad3090; noticeLoginFlag=1; remember_acct=1758027684%40qq.com; ts_uid=3109195288; bizuin=3544651505; rand_info=CAESIN9QUYc1/WK7rGJzwDIglhSSyfbmUCbuG+1t3+xAkXai; slave_bizuin=3544651505; data_bizuin=3544651505; data_ticket=w5SzyzyKmeHH+LvoW6ppd7banBn5TEK6cKg8XyUHTn/0y4bCLsDxLoCUaH01iJVD; slave_sid=WHdrOXNQaFlzZTdoWkk3UkRNNXZiX0dIWEJBSkowdzBWRXFQeEJneVh4T1U3S2J3aE1oU2VIYnNNSV9vdkNKR0lwcGxNWkUxYUt3YlFLVndFRjRVWFQ4N0JPczR0X0VHV3VvMWJ0aHk2STY1RDBWTzRtRHllRnpyRnhZRVpvdVFwQ1lTbzNhZ0pLNWUyanJQ; slave_user=gh_2320499b858e; xid=e91a08712c284033fbf2f567d67d52f9; openid2ticket_o8RZH00ax2nhUJiSJCYrJB1_zfSU=h/x9S5lPWmU67z7R3pFFqqU3gSla9aPRqHg8UcDa15k=\",\n",
    "  \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36\"}\n",
    "\n",
    "data = {\n",
    "    \"token\": \"2003980443\",\n",
    "    \"lang\": \"zh_CN\",\n",
    "    \"f\": \"json\",\n",
    "    \"ajax\": \"1\",\n",
    "    \"action\": \"list_ex\",\n",
    "    \"begin\": \"0\",\n",
    "    \"count\": \"5\",\n",
    "    \"query\": \"\",\n",
    "    \"fakeid\": \"MzIzNzYwMzM3Ng==\",\n",
    "    \"type\": \"9\",\n",
    "}\n",
    "\n",
    "\n",
    "\n",
    "content_list=[]\n",
    "\n",
    "for i in range(117):\n",
    "    data[\"begin\"] = i*5\n",
    "    print(data[\"begin\"])\n",
    "    time.sleep(2)\n",
    "    # 使用get方法进行提交\n",
    "    content_json = requests.get(url, headers=headers, params=data).json()\n",
    "#     print(content_json)\n",
    "    # 返回了一个json，里面是每一页的数据\n",
    "    for item in content_json[\"app_msg_list\"]:\n",
    "    # 提取每页文章的标题及对应的url\n",
    "        items = []\n",
    "        items.append(item[\"title\"])\n",
    "        items.append(item[\"link\"])\n",
    "        items.append(item[\"create_time\"])\n",
    "        content_list.append(items)\n",
    "\n",
    "\n",
    "name=['title','link','create_time']\n",
    "test=pd.DataFrame(columns=name,data=content_list)\n",
    "with pd.ExcelWriter(fn[\"output\"][\"公众号_xlsx\"].format(公众号=\"腾讯媒体研究院_requests\")) as writer:\n",
    "    test.to_excel(writer)\n",
    "\n",
    "# test.to_csv(\"../微信公众号爬虫_zhichao/南方周末.csv\",mode='a',encoding='utf-8')\n",
    "# print(\"保存成功\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 采集公众号（selenium）\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from lxml.html import fromstring\n",
    "import time\n",
    "from random import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\lenovo\\AppData\\Roaming\\Python\\Python37\\site-packages\\ipykernel_launcher.py:18: DeprecationWarning: use options instead of chrome_options\n"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.desired_capabilities import DesiredCapabilities\n",
    "\n",
    "#caps=dict()\n",
    "#caps[\"pageLoadStrategy\"] = \"none\"   # Do not wait for full page load\n",
    "\n",
    "opts = webdriver.ChromeOptions()\n",
    "opts.add_argument('--no-sandbox')#解决DevToolsActivePort文件不存在的报错\n",
    "opts.add_argument('window-size=1920x3000') #指定浏览器分辨率\n",
    "opts.add_argument('--disable-gpu') #谷歌文档提到需要加上一这个属性来规避bug\n",
    "opts.add_argument('--hide-scrollbars') #隐藏滚动条, 应对些特殊页面\n",
    "#opts.add_argument('blink-settings=imagesEnabled=false') #不加载图片, 提升速度\n",
    "#opts.add_argument('--headless') #浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败\n",
    "\n",
    "opts.binary_location = r\"C:\\Users\\lenovo\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe\" #\"H:\\_coding_\\Gitee\\InternetNewMedia\\CapstonePrj2016\\chromedriver.exe\"  \n",
    "\n",
    "# \"H:\\_coding_\\Gitee\\InternetNewMedia\\CapstonePrj2016\\chromedriver.exe\"  \n",
    "driver = webdriver.Chrome( chrome_options = opts) #desired_capabilities=caps,\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.get(\"https://mp.weixin.qq.com\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "#填表登入\n",
    "payload =  {\"account\": \"1758027684@qq.com\", \"password\": \"1721140jwjw\"}\n",
    "\n",
    "driver.find_element_by_xpath('//div[@class=\"login__type__container login__type__container__scan\"]/a').click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.find_element_by_xpath('//form[@class=\"login_form\"]//input[@name=\"account\"]').clear()\n",
    "driver.find_element_by_xpath('//form[@class=\"login_form\"]//input[@name=\"account\"]').send_keys(payload['account'])\n",
    "driver.find_element_by_xpath('//form[@class=\"login_form\"]//input[@name=\"password\"]').clear()\n",
    "driver.find_element_by_xpath('//form[@class=\"login_form\"]//input[@name=\"password\"]').send_keys(payload['password'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.find_element_by_xpath('//div[@class=\"login_btn_panel\"]/a').click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "element = driver.find_element_by_xpath('//a[@id=\"m_open\"]')\n",
    "element.click()\n",
    "main_content = element.get_attribute('innerHTML')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.execute_script(\"window.scrollTo(0,document.body.scrollHeight)\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'https://mp.weixin.qq.com/cgi-bin/appmsg?begin=0&count=10&t=media/appmsg_list&type=10&action=list&token=1159366768&lang=zh_CN'"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#点选单\n",
    "element = driver.find_element_by_xpath('//li[@title[contains(.,\"素材管理\")]]/a') \n",
    "# main_content = element.get_attribute('innerHTML')\n",
    "# main_content\n",
    "url2= element.get_attribute(\"href\")\n",
    "url2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.get(url2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "#新建图文消息\n",
    "element = driver.find_element_by_xpath('//*[text()[contains(.,\"新建图文消息\")]]') \n",
    "main_content = element.get_attribute('innerHTML')\n",
    "main_content\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['CDwindow-510CACCBC4E6492A846C09760AC1D5A8', 'CDwindow-DB2FC7AC79D688B43849B28C51AEEFB1']\n"
     ]
    }
   ],
   "source": [
    "print (driver.window_handles)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 新建图文消息开了另一分视窗，所以要切换 switch_to \n",
    "driver.switch_to.window(driver.window_handles[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                超链接              \n"
     ]
    }
   ],
   "source": [
    "#超链接\n",
    "element = driver.find_element_by_xpath('//*[text()[contains(.,\"超链接\")]]') \n",
    "main_content = element.get_attribute('innerHTML')\n",
    "print(main_content)\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "选择其他公众号\n"
     ]
    }
   ],
   "source": [
    "# 点 选择其他公众号\n",
    "element = driver.find_element_by_xpath('//*[text()[contains(.,\"选择其他公众号\")]]') \n",
    "main_content = element.get_attribute('innerHTML')\n",
    "print(main_content)\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.find_element_by_xpath('//form//div[@class=\"inner_link_account_area\"]//input[@class=\"weui-desktop-form__input\"]').clear()\n",
    "driver.find_element_by_xpath('//form//div[@class=\"inner_link_account_area\"]//input[@class=\"weui-desktop-form__input\"]').send_keys(公众号)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<div class=\"weui-desktop-icon weui-desktop-icon__inputSearch weui-desktop-icon__small\"><!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <svg width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M11.33 10.007l4.273 4.273a.502.502 0 0 1 .005.709l-.585.584a.499.499 0 0 1-.709-.004L10.046 11.3a6.278 6.278 0 1 1 1.284-1.294zm.012-3.729a5.063 5.063 0 1 0-10.127 0 5.063 5.063 0 0 0 10.127 0z\"></path></svg> <!----> <!----> <!----> <!----></div>\n"
     ]
    }
   ],
   "source": [
    "# 点放大镜搜\n",
    "element = driver.find_element_by_xpath('//button[@class=\"weui-desktop-icon-btn weui-desktop-search__btn\"]')\n",
    "main_content = element.get_attribute('innerHTML')\n",
    "print(main_content)\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<li class=\"inner_link_account_item\"><div class=\"weui-desktop-vm_primary\"><img src=\"http://mmbiz.qpic.cn/mmbiz_png/cdSLicavGUr23RakfD1j8csT3LMhYpOQ21ECXeZMpic1k1dTBWprafgazS97J5JaVCHNQr4ib0cuSGVDWc9rJUiatQ/0?wx_fmt=png\" class=\"inner_link_account_avatar\"> <strong class=\"inner_link_account_nickname\">腾讯媒体研究院</strong> <i class=\"inner_link_account_wechat\">微信号：TencentMRI</i></div> <div class=\"weui-desktop-vm_default inner_link_account_type\">订阅号</div></li><li class=\"inner_link_account_item\"><div class=\"weui-desktop-vm_primary\"><img src=\"http://mmbiz.qpic.cn/mmbiz_png/gRl4QnJRXWbgpDlacGvtd3icLOwstUj5eIRYLZyPVUBGV1BZAGjve7t7IX5tvLibMic2mwkiam9IibyDv50M02RNjxQ/0?wx_fmt=png\" class=\"inner_link_account_avatar\"> <strong class=\"inner_link_account_nickname\">腾讯芒种特训营</strong> <i class=\"inner_link_account_wechat\">微信号：mz_camp</i></div> <div class=\"weui-desktop-vm_default inner_link_account_type\">服务号</div></li><li class=\"inner_link_account_item\"><div class=\"weui-desktop-vm_primary\"><img src=\"http://mmbiz.qpic.cn/mmbiz_png/HH8uRF2v4zyt0JfL70RQGMOGbagyprO18gIJaSJnvnicMvxquKlUMbRd7bhWicA36EGlxiaka0eVQjyiaKsAHwiaUdA/0?wx_fmt=png\" class=\"inner_link_account_avatar\"> <strong class=\"inner_link_account_nickname\">全媒派</strong> <i class=\"inner_link_account_wechat\">微信号：quanmeipai</i></div> <div class=\"weui-desktop-vm_default inner_link_account_type\">订阅号</div></li><li class=\"inner_link_account_item\"><div class=\"weui-desktop-vm_primary\"><img src=\"http://mmbiz.qpic.cn/mmbiz_png/twfPKR2ibmy2vqxPpFfqbahicn4aiaibpSicfHkWfpImatAsGicdOuVRzVyKeGI7cQl56Rp5yf7tuZAIJzd73tyOoHdA/0?wx_fmt=png\" class=\"inner_link_account_avatar\"> <strong class=\"inner_link_account_nickname\">腾讯多媒体实验室</strong> <i class=\"inner_link_account_wechat\">微信号：TencentAVLab</i></div> <div class=\"weui-desktop-vm_default inner_link_account_type\">服务号</div></li><li class=\"inner_link_account_item\"><div class=\"weui-desktop-vm_primary\"><img src=\"http://mmsns.qpic.cn/mmsns/A0R6pEEjricaFJgpW6wLG3u0xRy0zzPPjrrfR5rhibPgyxEr3ibVhSODw/0\" class=\"inner_link_account_avatar\"> <strong class=\"inner_link_account_nickname\">天津市多媒体教育技术研究会</strong> <i class=\"inner_link_account_wechat\">微信号：m-edu-m</i></div> <div class=\"weui-desktop-vm_default inner_link_account_type\">订阅号</div></li>\n"
     ]
    }
   ],
   "source": [
    "element = driver.find_element_by_xpath('//ul[@class=\"inner_link_account_list\"]')\n",
    "main_content = element.get_attribute('innerHTML')\n",
    "print(main_content)\n",
    "公众号SERP = main_content\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 解析\n",
    "root = fromstring(公众号SERP) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>nickname</th>\n",
       "      <th>wechat</th>\n",
       "      <th>img</th>\n",
       "      <th>name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>腾讯媒体研究院</td>\n",
       "      <td>微信号：TencentMRI</td>\n",
       "      <td>http://mmbiz.qpic.cn/mmbiz_png/cdSLicavGUr23Ra...</td>\n",
       "      <td>订阅号</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>腾讯芒种特训营</td>\n",
       "      <td>微信号：mz_camp</td>\n",
       "      <td>http://mmbiz.qpic.cn/mmbiz_png/gRl4QnJRXWbgpDl...</td>\n",
       "      <td>服务号</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>全媒派</td>\n",
       "      <td>微信号：quanmeipai</td>\n",
       "      <td>http://mmbiz.qpic.cn/mmbiz_png/HH8uRF2v4zyt0Jf...</td>\n",
       "      <td>订阅号</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>腾讯多媒体实验室</td>\n",
       "      <td>微信号：TencentAVLab</td>\n",
       "      <td>http://mmbiz.qpic.cn/mmbiz_png/twfPKR2ibmy2vqx...</td>\n",
       "      <td>服务号</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>天津市多媒体教育技术研究会</td>\n",
       "      <td>微信号：m-edu-m</td>\n",
       "      <td>http://mmsns.qpic.cn/mmsns/A0R6pEEjricaFJgpW6w...</td>\n",
       "      <td>订阅号</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        nickname            wechat  \\\n",
       "0        腾讯媒体研究院    微信号：TencentMRI   \n",
       "1        腾讯芒种特训营       微信号：mz_camp   \n",
       "2            全媒派    微信号：quanmeipai   \n",
       "3       腾讯多媒体实验室  微信号：TencentAVLab   \n",
       "4  天津市多媒体教育技术研究会       微信号：m-edu-m   \n",
       "\n",
       "                                                 img name  \n",
       "0  http://mmbiz.qpic.cn/mmbiz_png/cdSLicavGUr23Ra...  订阅号  \n",
       "1  http://mmbiz.qpic.cn/mmbiz_png/gRl4QnJRXWbgpDl...  服务号  \n",
       "2  http://mmbiz.qpic.cn/mmbiz_png/HH8uRF2v4zyt0Jf...  订阅号  \n",
       "3  http://mmbiz.qpic.cn/mmbiz_png/twfPKR2ibmy2vqx...  服务号  \n",
       "4  http://mmsns.qpic.cn/mmsns/A0R6pEEjricaFJgpW6w...  订阅号  "
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "主 = root.xpath('//li[@class=\"inner_link_account_item\"]')\n",
    "\n",
    "account_list = []\n",
    "for e in 主:\n",
    "    account_nickname = e.xpath('./div/strong[@class=\"inner_link_account_nickname\"]')[0].text\n",
    "    account_wechat = e.xpath('./div/i[@class=\"inner_link_account_wechat\"]')[0].text\n",
    "    account_img = e.xpath('./div/img/@src')[0]\n",
    "    account_name= e.xpath('./div[@class=\"weui-desktop-vm_default inner_link_account_type\"]')[0].text\n",
    "    account = {\"nickname\": account_nickname, \"wechat\": account_wechat, \"img\": account_img,\"name\":account_name}\n",
    "    account_list.append(account)\n",
    "\n",
    "df_account = pd.DataFrame(account_list)\n",
    "df_account"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<div class=\"weui-desktop-vm_primary\"><img src=\"http://mmbiz.qpic.cn/mmbiz_png/cdSLicavGUr23RakfD1j8csT3LMhYpOQ21ECXeZMpic1k1dTBWprafgazS97J5JaVCHNQr4ib0cuSGVDWc9rJUiatQ/0?wx_fmt=png\" class=\"inner_link_account_avatar\"> <strong class=\"inner_link_account_nickname\">腾讯媒体研究院</strong> <i class=\"inner_link_account_wechat\">微信号：TencentMRI</i></div> <div class=\"weui-desktop-vm_default inner_link_account_type\">订阅号</div>\n"
     ]
    }
   ],
   "source": [
    "element = driver.find_element_by_xpath('//ul[@class=\"inner_link_account_list\"]/li')\n",
    "main_content = element.get_attribute('innerHTML')\n",
    "print(main_content)\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n跳转_input = driver.find_element_by_xpath(\\'//span[@class=\"weui-desktop-pagination__form\"]/input\\')\\n跳转_a = driver.find_element_by_xpath(\\'//span[@class=\"weui-desktop-pagination__form\"]/a\\')\\n跳转_input.clear()\\n跳转_input.send_keys(117)\\n跳转_a.click()\\n'"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 跳转testing\n",
    "'''\n",
    "跳转_input = driver.find_element_by_xpath('//span[@class=\"weui-desktop-pagination__form\"]/input')\n",
    "跳转_a = driver.find_element_by_xpath('//span[@class=\"weui-desktop-pagination__form\"]/a')\n",
    "跳转_input.clear()\n",
    "跳转_input.send_keys(117)\n",
    "跳转_a.click()\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 117]\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 跳转上限\n",
    "l_e = driver.find_elements_by_xpath('//label[@class=\"weui-desktop-pagination__num\"]')\n",
    "l_e_int  = [int(x.text) for x in l_e] \n",
    "print (l_e_int)\n",
    "print (l_e_int[0]==l_e_int[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117]\n"
     ]
    }
   ],
   "source": [
    "pages = list(range(l_e_int[0],l_e_int[-1]+1 ))\n",
    "#print(pages[0:2])\n",
    "pages = list(range(1,l_e_int[-1]+1 ))\n",
    "print(pages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "#循环遍历所有页数\n",
    "# global varialbes \n",
    "html_raw = dict()\n",
    "main_content =\"\"\n",
    "element = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "def process_pages (pages):\n",
    "    for p in pages:\n",
    "        print (p,end='\\t')\n",
    "\n",
    "        跳转_input = driver.find_element_by_xpath('//span[@class=\"weui-desktop-pagination__form\"]/input')\n",
    "        跳转_a = driver.find_element_by_xpath('//span[@class=\"weui-desktop-pagination__form\"]/a')\n",
    "        跳转_input.clear()\n",
    "        跳转_input.send_keys(p)\n",
    "        跳转_a.click()\n",
    "\n",
    "        time.sleep(20*random())\n",
    "\n",
    "        element = driver.find_element_by_xpath('//div[@class=\"inner_link_article_list\"]')\n",
    "        main_content = element.get_attribute('innerHTML')\n",
    "        #print(main_content)\n",
    "        html_raw[p] = main_content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\t2\t3\t4\t5\t6\t7\t8\t9\t10\t11\t12\t13\t14\t15\t16\t17\t18\t19\t20\t21\t22\t23\t24\t25\t26\t27\t28\t29\t30\t31\t32\t33\t34\t35\t36\t37\t38\t39\t40\t41\t42\t43\t44\t45\t46\t47\t48\t49\t50\t51\t52\t53\t54\t55\t56\t57\t58\t59\t60\t61\t62\t63\t64\t65\t66\t67\t68\t69\t70\t71\t72\t73\t74\t75\t76\t77\t78\t79\t80\t81\t82\t83\t84\t85\t86\t87\t88\t89\t90\t91\t92\t93\t94\t95\t96\t97\t98\t99\t100\t101\t102\t103\t104\t105\t106\t107\t108\t109\t110\t111\t112\t113\t114\t115\t116\t117\t"
     ]
    }
   ],
   "source": [
    "process_pages(pages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>html_snippets</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>113</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>114</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>115</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>116</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>117</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>117 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                         html_snippets\n",
       "1    <div><label class=\"inner_link_article_item\"><s...\n",
       "2    <div><label class=\"inner_link_article_item\"><s...\n",
       "3    <div><label class=\"inner_link_article_item\"><s...\n",
       "4    <div><label class=\"inner_link_article_item\"><s...\n",
       "5    <div><label class=\"inner_link_article_item\"><s...\n",
       "..                                                 ...\n",
       "113  <div><label class=\"inner_link_article_item\"><s...\n",
       "114  <div><label class=\"inner_link_article_item\"><s...\n",
       "115  <div><label class=\"inner_link_article_item\"><s...\n",
       "116  <div><label class=\"inner_link_article_item\"><s...\n",
       "117  <div><label class=\"inner_link_article_item\"><s...\n",
       "\n",
       "[117 rows x 1 columns]"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame([html_raw]).T\n",
    "df.columns = [\"html_snippets\"]\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Stored 'html_raw' (dict)\n"
     ]
    }
   ],
   "source": [
    "%store html_raw\n",
    "import pickle \n",
    "filehandler = open(\"html_raw\", 'wb') \n",
    "pickle.dump(html_raw, filehandler)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "80\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>html_snippets</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>81</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>83</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>84</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>85</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>86</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>87</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>88</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>89</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>90</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>91</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>92</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>93</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>94</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>100</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>101</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>103</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>104</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>105</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>106</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>107</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>108</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>109</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>110</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>111</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>112</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>113</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>114</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>115</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>116</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>117</th>\n",
       "      <td>&lt;div&gt;&lt;label class=\"inner_link_article_item\"&gt;&lt;s...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                         html_snippets\n",
       "81   <div><label class=\"inner_link_article_item\"><s...\n",
       "82   <div><label class=\"inner_link_article_item\"><s...\n",
       "83   <div><label class=\"inner_link_article_item\"><s...\n",
       "84   <div><label class=\"inner_link_article_item\"><s...\n",
       "85   <div><label class=\"inner_link_article_item\"><s...\n",
       "86   <div><label class=\"inner_link_article_item\"><s...\n",
       "87   <div><label class=\"inner_link_article_item\"><s...\n",
       "88   <div><label class=\"inner_link_article_item\"><s...\n",
       "89   <div><label class=\"inner_link_article_item\"><s...\n",
       "90   <div><label class=\"inner_link_article_item\"><s...\n",
       "91   <div><label class=\"inner_link_article_item\"><s...\n",
       "92   <div><label class=\"inner_link_article_item\"><s...\n",
       "93   <div><label class=\"inner_link_article_item\"><s...\n",
       "94   <div><label class=\"inner_link_article_item\"><s...\n",
       "95   <div><label class=\"inner_link_article_item\"><s...\n",
       "96   <div><label class=\"inner_link_article_item\"><s...\n",
       "97   <div><label class=\"inner_link_article_item\"><s...\n",
       "98   <div><label class=\"inner_link_article_item\"><s...\n",
       "99   <div><label class=\"inner_link_article_item\"><s...\n",
       "100  <div><label class=\"inner_link_article_item\"><s...\n",
       "101  <div><label class=\"inner_link_article_item\"><s...\n",
       "102  <div><label class=\"inner_link_article_item\"><s...\n",
       "103  <div><label class=\"inner_link_article_item\"><s...\n",
       "104  <div><label class=\"inner_link_article_item\"><s...\n",
       "105  <div><label class=\"inner_link_article_item\"><s...\n",
       "106  <div><label class=\"inner_link_article_item\"><s...\n",
       "107  <div><label class=\"inner_link_article_item\"><s...\n",
       "108  <div><label class=\"inner_link_article_item\"><s...\n",
       "109  <div><label class=\"inner_link_article_item\"><s...\n",
       "110  <div><label class=\"inner_link_article_item\"><s...\n",
       "111  <div><label class=\"inner_link_article_item\"><s...\n",
       "112  <div><label class=\"inner_link_article_item\"><s...\n",
       "113  <div><label class=\"inner_link_article_item\"><s...\n",
       "114  <div><label class=\"inner_link_article_item\"><s...\n",
       "115  <div><label class=\"inner_link_article_item\"><s...\n",
       "116  <div><label class=\"inner_link_article_item\"><s...\n",
       "117  <div><label class=\"inner_link_article_item\"><s..."
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_out = df[~df.duplicated()]\n",
    "print (len(df_out))\n",
    "df[df.duplicated()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[81,\n",
       " 82,\n",
       " 83,\n",
       " 84,\n",
       " 85,\n",
       " 86,\n",
       " 87,\n",
       " 88,\n",
       " 89,\n",
       " 90,\n",
       " 91,\n",
       " 92,\n",
       " 93,\n",
       " 94,\n",
       " 95,\n",
       " 96,\n",
       " 97,\n",
       " 98,\n",
       " 99,\n",
       " 100,\n",
       " 101,\n",
       " 102,\n",
       " 103,\n",
       " 104,\n",
       " 105,\n",
       " 106,\n",
       " 107,\n",
       " 108,\n",
       " 109,\n",
       " 110,\n",
       " 111,\n",
       " 112,\n",
       " 113,\n",
       " 114,\n",
       " 115,\n",
       " 116,\n",
       " 117]"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "try_again = list(df[df.duplicated()].index)\n",
    "print(try_again)\n",
    "try_again = try_again + list (set(pages).difference(set(df.index.values)))\n",
    "try_again"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 暂存档\n",
    "filename = fn [\"output\"] [\"公众号_htm_snippets\"] \n",
    "df_out.to_csv(filename.format(公众号=公众号), sep=\"\\t\", encoding=\"utf8\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6,9,8,5,6,6,5,5,5,5,6,7,6,5,9,7,6,6,5,6,6,5,6,5,5,6,6,6,7,5,5,5,6,6,5,5,5,5,5,6,6,6,8,5,6,6,6,5,6,6,7,6,7,11,5,5,5,6,9,6,5,5,5,10,5,6,6,9,6,5,5,10,11,5,5,5,5,6,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,"
     ]
    }
   ],
   "source": [
    "def parse_html_snippets(_snippet_):\n",
    "    root = fromstring(_snippet_) \n",
    "    title = [x.text for x in root.xpath('//div[@class=\"inner_link_article_title\"]')]\n",
    "    create_time = [x.text for x in root.xpath('//div[@class=\"inner_link_article_date\"]')]\n",
    "    link = [x for x in root.xpath('//a/@href')]\n",
    "    _df_ = pd.DataFrame({\"title\":title, \"create_time\": create_time, \"link\":link})\n",
    "    return(_df_)\n",
    "    \n",
    "l_df = []\n",
    "for p in pages:\n",
    "    _df_ = parse_html_snippets(df.loc[p,\"html_snippets\"])\n",
    "    print (len(_df_), end=\",\")\n",
    "    l_df.append(_df_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>title</th>\n",
       "      <th>create_time</th>\n",
       "      <th>link</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>媒体融合语境下的短视频丨【芒种·观点】</td>\n",
       "      <td>2020-05-15</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>腾讯新闻热点运营主编邱帅：爆款资讯短视频的运营逻辑丨【芒种·观点】</td>\n",
       "      <td>2020-05-14</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>腾讯深网主编范晓东：科技大佬访谈的运营之道丨课程预告</td>\n",
       "      <td>2020-05-14</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>如何频繁产出爆款新闻短视频？丨【芒种·观点】</td>\n",
       "      <td>2020-05-13</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>你所追寻的“价值”是什么？</td>\n",
       "      <td>2020-05-12</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>702</th>\n",
       "      <td>人工智能会将新闻媒体带向何方？看看纽约时报、路透社和其他媒体巨头的人工智能应用丨【芒种·趋势】</td>\n",
       "      <td>2018-08-31</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>703</th>\n",
       "      <td>176位专家对新闻业的预测：媒体未来的8大趋势丨【芒种·趋势】</td>\n",
       "      <td>2018-08-29</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>704</th>\n",
       "      <td>三四线用户互联网生活报告：娱乐消遣、追随从众丨【芒种·报告】</td>\n",
       "      <td>2018-08-28</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>705</th>\n",
       "      <td>方军：我们可能低估了知识产品的媒体属性丨【芒种·观点】</td>\n",
       "      <td>2018-08-27</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>706</th>\n",
       "      <td>今天，首都媒体界有一件大事！北京广播电视台融媒体中心成立啦！</td>\n",
       "      <td>2018-08-27</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>707 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                               title create_time  \\\n",
       "0                                媒体融合语境下的短视频丨【芒种·观点】  2020-05-15   \n",
       "1                  腾讯新闻热点运营主编邱帅：爆款资讯短视频的运营逻辑丨【芒种·观点】  2020-05-14   \n",
       "2                         腾讯深网主编范晓东：科技大佬访谈的运营之道丨课程预告  2020-05-14   \n",
       "3                             如何频繁产出爆款新闻短视频？丨【芒种·观点】  2020-05-13   \n",
       "4                                      你所追寻的“价值”是什么？  2020-05-12   \n",
       "..                                               ...         ...   \n",
       "702  人工智能会将新闻媒体带向何方？看看纽约时报、路透社和其他媒体巨头的人工智能应用丨【芒种·趋势】  2018-08-31   \n",
       "703                  176位专家对新闻业的预测：媒体未来的8大趋势丨【芒种·趋势】  2018-08-29   \n",
       "704                   三四线用户互联网生活报告：娱乐消遣、追随从众丨【芒种·报告】  2018-08-28   \n",
       "705                      方军：我们可能低估了知识产品的媒体属性丨【芒种·观点】  2018-08-27   \n",
       "706                   今天，首都媒体界有一件大事！北京广播电视台融媒体中心成立啦！  2018-08-27   \n",
       "\n",
       "                                                  link  \n",
       "0    http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...  \n",
       "1    http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...  \n",
       "2    http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...  \n",
       "3    http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...  \n",
       "4    http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...  \n",
       "..                                                 ...  \n",
       "702  http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...  \n",
       "703  http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...  \n",
       "704  http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...  \n",
       "705  http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...  \n",
       "706  http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...  \n",
       "\n",
       "[707 rows x 3 columns]"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_url_out = pd.concat(l_df).reset_index(drop=True)\n",
    "df_url_out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>title</th>\n",
       "      <th>create_time</th>\n",
       "      <th>link</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>value</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>如何频繁产出爆款新闻短视频？丨【芒种·观点】</td>\n",
       "      <td>2020-05-13</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>你所追寻的“价值”是什么？</td>\n",
       "      <td>2020-05-12</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>专业至上，价值为王：知识问答类产品用户需求洞察报告丨【芒种·报告】</td>\n",
       "      <td>2020-05-11</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>第一财经总编辑杨宇东等：解析抗疫报道的“一财”战法丨传媒前线</td>\n",
       "      <td>2020-05-09</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>腾讯网副总编辑杨瑞春：如何以娱乐外壳打造质感表达？丨【芒种·案例】</td>\n",
       "      <td>2020-05-08</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>469</th>\n",
       "      <td>5招解码场景写作，重塑故事的灵魂丨【芒种·观点】</td>\n",
       "      <td>2018-09-17</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>472</th>\n",
       "      <td>樊登：创业最重要的是掌握“反脆弱”的智慧丨【芒种·观点】</td>\n",
       "      <td>2018-09-13</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>474</th>\n",
       "      <td>樊登读书创始人亲述：如何打造0到50亿社群增长丨2018内容创新创业公开课第一期</td>\n",
       "      <td>2018-09-10</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>475</th>\n",
       "      <td>控“声”法则：合理操纵声音激发受众想象力丨【芒种·观点】</td>\n",
       "      <td>2018-09-07</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>478</th>\n",
       "      <td>1818黄金眼：欢乐源泉or叛逆先锋丨【芒种·案例】</td>\n",
       "      <td>2018-09-04</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>347 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                          title create_time  \\\n",
       "value                                                         \n",
       "3                        如何频繁产出爆款新闻短视频？丨【芒种·观点】  2020-05-13   \n",
       "4                                 你所追寻的“价值”是什么？  2020-05-12   \n",
       "5             专业至上，价值为王：知识问答类产品用户需求洞察报告丨【芒种·报告】  2020-05-11   \n",
       "6                第一财经总编辑杨宇东等：解析抗疫报道的“一财”战法丨传媒前线  2020-05-09   \n",
       "7             腾讯网副总编辑杨瑞春：如何以娱乐外壳打造质感表达？丨【芒种·案例】  2020-05-08   \n",
       "...                                         ...         ...   \n",
       "469                    5招解码场景写作，重塑故事的灵魂丨【芒种·观点】  2018-09-17   \n",
       "472                樊登：创业最重要的是掌握“反脆弱”的智慧丨【芒种·观点】  2018-09-13   \n",
       "474    樊登读书创始人亲述：如何打造0到50亿社群增长丨2018内容创新创业公开课第一期  2018-09-10   \n",
       "475                控“声”法则：合理操纵声音激发受众想象力丨【芒种·观点】  2018-09-07   \n",
       "478                  1818黄金眼：欢乐源泉or叛逆先锋丨【芒种·案例】  2018-09-04   \n",
       "\n",
       "                                                    link  \n",
       "value                                                     \n",
       "3      http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...  \n",
       "4      http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...  \n",
       "5      http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...  \n",
       "6      http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...  \n",
       "7      http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...  \n",
       "...                                                  ...  \n",
       "469    http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...  \n",
       "472    http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...  \n",
       "474    http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...  \n",
       "475    http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...  \n",
       "478    http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...  \n",
       "\n",
       "[347 rows x 3 columns]"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# tagging 标记\n",
    "tagging_list = [\"\",\"媒体\",\"互联网\",\"运营\",\"发展\"] #overwritable\n",
    "\n",
    "v_v_list = []\n",
    "\n",
    "for tag in tagging_list:\n",
    "    index_list = df_url_out [ df_url_out.title.str.contains(tag) ].index.tolist()\n",
    "    v_v_pairs = pd.DataFrame({tag:index_list}).melt().set_index(\"value\")\n",
    "    v_v_list.append(v_v_pairs)\n",
    "\n",
    "df_cat = v_v_list[0]\n",
    "for d in v_v_list:\n",
    "    df_cat.update(d)\n",
    "    \n",
    "# 尚未标记内容\n",
    "df_url_out.loc [ df_cat.query('variable==\"\"').index ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>title</th>\n",
       "      <th>create_time</th>\n",
       "      <th>link</th>\n",
       "      <th>variable</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>媒体融合语境下的短视频丨【芒种·观点】</td>\n",
       "      <td>2020-05-15</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "      <td>媒体</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>腾讯新闻热点运营主编邱帅：爆款资讯短视频的运营逻辑丨【芒种·观点】</td>\n",
       "      <td>2020-05-14</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "      <td>运营</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>腾讯深网主编范晓东：科技大佬访谈的运营之道丨课程预告</td>\n",
       "      <td>2020-05-14</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "      <td>运营</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>如何频繁产出爆款新闻短视频？丨【芒种·观点】</td>\n",
       "      <td>2020-05-13</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "      <td>无分类</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>你所追寻的“价值”是什么？</td>\n",
       "      <td>2020-05-12</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "      <td>无分类</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>702</th>\n",
       "      <td>人工智能会将新闻媒体带向何方？看看纽约时报、路透社和其他媒体巨头的人工智能应用丨【芒种·趋势】</td>\n",
       "      <td>2018-08-31</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "      <td>媒体</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>703</th>\n",
       "      <td>176位专家对新闻业的预测：媒体未来的8大趋势丨【芒种·趋势】</td>\n",
       "      <td>2018-08-29</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "      <td>媒体</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>704</th>\n",
       "      <td>三四线用户互联网生活报告：娱乐消遣、追随从众丨【芒种·报告】</td>\n",
       "      <td>2018-08-28</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "      <td>互联网</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>705</th>\n",
       "      <td>方军：我们可能低估了知识产品的媒体属性丨【芒种·观点】</td>\n",
       "      <td>2018-08-27</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "      <td>媒体</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>706</th>\n",
       "      <td>今天，首都媒体界有一件大事！北京广播电视台融媒体中心成立啦！</td>\n",
       "      <td>2018-08-27</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "      <td>媒体</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>707 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                               title create_time  \\\n",
       "0                                媒体融合语境下的短视频丨【芒种·观点】  2020-05-15   \n",
       "1                  腾讯新闻热点运营主编邱帅：爆款资讯短视频的运营逻辑丨【芒种·观点】  2020-05-14   \n",
       "2                         腾讯深网主编范晓东：科技大佬访谈的运营之道丨课程预告  2020-05-14   \n",
       "3                             如何频繁产出爆款新闻短视频？丨【芒种·观点】  2020-05-13   \n",
       "4                                      你所追寻的“价值”是什么？  2020-05-12   \n",
       "..                                               ...         ...   \n",
       "702  人工智能会将新闻媒体带向何方？看看纽约时报、路透社和其他媒体巨头的人工智能应用丨【芒种·趋势】  2018-08-31   \n",
       "703                  176位专家对新闻业的预测：媒体未来的8大趋势丨【芒种·趋势】  2018-08-29   \n",
       "704                   三四线用户互联网生活报告：娱乐消遣、追随从众丨【芒种·报告】  2018-08-28   \n",
       "705                      方军：我们可能低估了知识产品的媒体属性丨【芒种·观点】  2018-08-27   \n",
       "706                   今天，首都媒体界有一件大事！北京广播电视台融媒体中心成立啦！  2018-08-27   \n",
       "\n",
       "                                                  link variable  \n",
       "0    http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...       媒体  \n",
       "1    http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...       运营  \n",
       "2    http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...       运营  \n",
       "3    http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...      无分类  \n",
       "4    http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...      无分类  \n",
       "..                                                 ...      ...  \n",
       "702  http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...       媒体  \n",
       "703  http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...       媒体  \n",
       "704  http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...      互联网  \n",
       "705  http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...       媒体  \n",
       "706  http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...       媒体  \n",
       "\n",
       "[707 rows x 4 columns]"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_o = df_url_out.join(df_cat).replace(\"\", np.nan).fillna(\"无分类\")\n",
    "df_o"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>title</th>\n",
       "      <th>create_time</th>\n",
       "      <th>link</th>\n",
       "      <th>variable</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>媒体融合语境下的短视频丨【芒种·观点】</td>\n",
       "      <td>2020-05-15</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "      <td>媒体</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>2020普利策奖公布：严肃媒体依旧是最大赢家</td>\n",
       "      <td>2020-05-06</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "      <td>媒体</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>“后疫情”时代智媒体如何破局？封面传媒李鹏分享智媒新路线丨【芒种·观点】</td>\n",
       "      <td>2020-04-29</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "      <td>媒体</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>媒体进入“后平台时代”：全球新闻业更关注自身独立性丨【芒种·报告】</td>\n",
       "      <td>2020-04-24</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "      <td>媒体</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>2019中国网络媒体社会价值白皮书丨【芒种·报告】</td>\n",
       "      <td>2020-04-21</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "      <td>媒体</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>701</th>\n",
       "      <td>美联社报告：数字时代媒体创收新途径 丨【芒种·报告】</td>\n",
       "      <td>2018-09-03</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "      <td>媒体</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>702</th>\n",
       "      <td>人工智能会将新闻媒体带向何方？看看纽约时报、路透社和其他媒体巨头的人工智能应用丨【芒种·趋势】</td>\n",
       "      <td>2018-08-31</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "      <td>媒体</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>703</th>\n",
       "      <td>176位专家对新闻业的预测：媒体未来的8大趋势丨【芒种·趋势】</td>\n",
       "      <td>2018-08-29</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "      <td>媒体</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>705</th>\n",
       "      <td>方军：我们可能低估了知识产品的媒体属性丨【芒种·观点】</td>\n",
       "      <td>2018-08-27</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "      <td>媒体</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>706</th>\n",
       "      <td>今天，首都媒体界有一件大事！北京广播电视台融媒体中心成立啦！</td>\n",
       "      <td>2018-08-27</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...</td>\n",
       "      <td>媒体</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>287 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                               title create_time  \\\n",
       "0                                媒体融合语境下的短视频丨【芒种·观点】  2020-05-15   \n",
       "9                             2020普利策奖公布：严肃媒体依旧是最大赢家  2020-05-06   \n",
       "11              “后疫情”时代智媒体如何破局？封面传媒李鹏分享智媒新路线丨【芒种·观点】  2020-04-29   \n",
       "17                 媒体进入“后平台时代”：全球新闻业更关注自身独立性丨【芒种·报告】  2020-04-24   \n",
       "21                         2019中国网络媒体社会价值白皮书丨【芒种·报告】  2020-04-21   \n",
       "..                                               ...         ...   \n",
       "701                       美联社报告：数字时代媒体创收新途径 丨【芒种·报告】  2018-09-03   \n",
       "702  人工智能会将新闻媒体带向何方？看看纽约时报、路透社和其他媒体巨头的人工智能应用丨【芒种·趋势】  2018-08-31   \n",
       "703                  176位专家对新闻业的预测：媒体未来的8大趋势丨【芒种·趋势】  2018-08-29   \n",
       "705                      方军：我们可能低估了知识产品的媒体属性丨【芒种·观点】  2018-08-27   \n",
       "706                   今天，首都媒体界有一件大事！北京广播电视台融媒体中心成立啦！  2018-08-27   \n",
       "\n",
       "                                                  link variable  \n",
       "0    http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...       媒体  \n",
       "9    http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...       媒体  \n",
       "11   http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...       媒体  \n",
       "17   http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...       媒体  \n",
       "21   http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...       媒体  \n",
       "..                                                 ...      ...  \n",
       "701  http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...       媒体  \n",
       "702  http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...       媒体  \n",
       "703  http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...       媒体  \n",
       "705  http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...       媒体  \n",
       "706  http://mp.weixin.qq.com/s?__biz=MzIzNzYwMzM3Ng...       媒体  \n",
       "\n",
       "[287 rows x 4 columns]"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_o[df_o.title.str.contains(\"媒体\")]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>title</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>variable</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>无分类</th>\n",
       "      <td>347</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>媒体</th>\n",
       "      <td>271</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>互联网</th>\n",
       "      <td>51</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>发展</th>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>运营</th>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          title\n",
       "variable       \n",
       "无分类         347\n",
       "媒体          271\n",
       "互联网          51\n",
       "发展           19\n",
       "运营           19"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_stats = df_o.groupby(by=\"variable\").agg({\"title\":\"count\"}).sort_values(by=\"title\", ascending=False)\n",
    "df_stats"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [],
   "source": [
    "#输出\n",
    "df_account.columns.name = \"rel_accounts\"\n",
    "df_o.columns.name = \"url_cat\"\n",
    "df_stats.columns.name = \"stats\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [],
   "source": [
    "_df_.columns.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get the xlsxwriter workbook and worksheet objects.  \n",
    "with pd.ExcelWriter(fn[\"output\"][\"公众号_xlsx\"].format(公众号=公众号)) as writer:\n",
    "    workbook  = writer.book\n",
    "\n",
    "    for _df_ in [df_account, df_o, df_stats]:\n",
    "        _df_.to_excel(writer, sheet_name = _df_.columns.name)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
